package com.hs.singleton;

import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * @Author: hs
 * @Description: 双重检查[推荐用]  优点：线程安全；延迟加载；效率较高。
 * @Date: Create in 18:53 2019/7/28
 */
public class SingletonDoubleCheck  implements Serializable {

    private static final long serialVersionUID = 888L;


    private static volatile SingletonDoubleCheck singletonDoubleCheck;

    private SingletonDoubleCheck() {}

    public static SingletonDoubleCheck getInstance() {
        if (singletonDoubleCheck == null) {
            synchronized (SingletonDoubleCheck.class) {
                if (singletonDoubleCheck == null) {
                    singletonDoubleCheck = new SingletonDoubleCheck();
                }
            }
        }
        return singletonDoubleCheck;
    }

    protected Object readResolve() throws ObjectStreamException {
        System.out.println("调用了readResolve方法！");
        return singletonDoubleCheck;
    }

}